﻿#region namespaces

using System;
using System.IO;
using System.Web.UI.WebControls;

using NotesFor;

using SLIITPro.Core;
using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

#endregion

namespace SLIITPro.SysUsers
{
    public partial class Locations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpload.Click += new EventHandler(btnUpload_Click);
            if (!Page.IsPostBack)
            {
                Utility.BindCountries(cmbCountry);
            }

        }

        private BusinessItem _businessItem;
        private Users _users;
        private BusinessItemExtension _itemExtension;
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Page.Validate("B");
            if (Page.IsValid)
            {
                _users = new Users(new UserExtension().SelectUser("Email", User.Identity.Name).Rows[0]);
                _businessItem = new BusinessItem
                                    {
                                        BUId = _users.UserId,
                                        BName = txtBusinessName.Text.Trim(),
                                        BCity = txtBusinessCity.Text.Trim(),
                                        BCountry = cmbCountry.SelectedValue,
                                        BEmail = txtBusinessEmail.Text.Trim(),
                                        BPhone = txtBusinessPhone.Text.Trim(),
                                        BStreet = txtBusinessStreet.Text.Trim(),
                                        BWeb = txtBusinessWeb.Text.Trim(),
                                        BType = cmbBusinessType.SelectedValue,
                                        BCreatedAt = DateTime.Now,
                                        BUpdatedAt = DateTime.Now,
                                        BImageFolderID =
                                            Session["entry"] != null ? Session["entry"].ToString() : string.Empty
                                    };
                _itemExtension = new BusinessItemExtension();
                try
                {
                    _itemExtension.CreateBusinessItem(_businessItem);
                    lblMessage.Text = "Business added succeeded.";
                    Session.Remove("entry");
                    ImageAlbum.Controls.Clear();
                    Utility.ClearContent(regBusiness);
                }
                catch (Exception ex)
                {
                    lblMessage.Text = "Error Occured !!" + ex.Message;
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (Session["entry"] == null)
                Session.Add("entry", Utility.GenerateId());
            var filename = ImgUpload.FileName;
            var filepath = CoreSettings.GlobalBusinessImageRoot + Session["entry"] + "/";
            Upload(filepath, ImgUpload, filename);
            var galleryPro = new Gallery { FolderUrl = filepath, Size = 100 };
            ImageAlbum.Controls.Add(galleryPro);
        }

        private void Upload(string virtualFolder, FileUpload control, string fileName)
        {
            string folder = Server.MapPath(virtualFolder);
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);
            if (control.HasFile)
            {
                control.PostedFile.SaveAs(folder + fileName);
            }
        }
    }
}
